package com.yanqu.road.server.cmd.activity.stronghold.eliminate;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.StrongholdPatronsData;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_STRONGHOLD_WAR_ELIMINATION_ATTACK, desc = "挑战联盟")
public class StrongholdChallengeUnionCmd extends AbstractCommand {
    private int id;
    private int patronsId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.AttackEliminatePatronsReqMsg req = StrongholdWarProto.AttackEliminatePatronsReqMsg.parseFrom(packet.getBytes());
        id = req.getId();
        patronsId = req.getPatronsId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        Map<Integer, UserPatrons> userPatronsMap = player.getModule(PatronsModule.class).getUserPatronsMap();
        UserPatrons patrons = userPatronsMap.get(patronsId);
        if (patrons == null) {
            return;
        }
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
            sendToPlayer(player);
            return;
        }
        if (!StrongholdActivityMgr.isEliminateTime()) {
            ret = GameErrorCode.E_STRONGHOLD_ELIMINATE_NO_OPEN;
            sendToPlayer(player);
            return;
        }
       /* List<Integer> eliminateDefendPatrons = StrongholdWarMgr.getEliminateDefendPatrons(activityInfo.getActivityId(), player.getUserInfo().getUnionUid(), player.getUserId());
        if (eliminateDefendPatrons.contains(patronsId)) {
            ret = GameErrorCode.E_STRONGHOLD_DEFEND_PATRONS_CAN_ATTACK;
            sendToPlayer(player);
            return;
        }*/
        StrongholdModule strongholdModule = player.getModule(StrongholdModule.class);
        strongholdModule.resetBattlePatrons();
        Map<Integer, StrongholdPatronsData> dispatchMap = strongholdModule.getDispatchMap(activityInfo.getActivityId());
        Property consume = null;
        if (dispatchMap != null) {
            StrongholdPatronsData data = dispatchMap.get(patronsId);
            if (data != null) {
                if (data.getFreeTimes() >= StrongholdActivityMgr.getEliminateFreeTimes()) {
                    int buyTimes = data.getBuyTimes();
                    if (buyTimes >= StrongholdActivityMgr.getEliminateBuyTimes()) {
                        ret = GameErrorCode.E_STRONGHOLD_PATRONS_BATTLE_TIMES_MAX;
                        sendToPlayer(player);
                        return;
                    }
                     consume = StrongholdActivityMgr.getEliminateConsume(buyTimes);
                    if (!player.getModule(CurrencyModule.class).currencyIsEnough(consume)) {
                        ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                        sendToPlayer(player);
                        return;
                    }
                }
            }
        }
        if (consume != null) {
            player.getModule(PreReduceModule.class).preReduce(PropertyHelper.parsePropertyToString(consume));
        }
        StrongholdWarProto.CrossAttackEliminatePatronsReqMsg.Builder req = StrongholdWarProto.CrossAttackEliminatePatronsReqMsg.newBuilder();
        req.setActivityId(activityInfo.getActivityId());
        req.setId(id);
        req.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        req.setPatronsId(patronsId);
        long otherAddition = StrongholdWarMgr.getOtherAddition(player);
        BigInteger totalAbility =  StrongholdWarMgr.calcStrongholdPatronsTotalAbility(patrons, userPatronsMap, otherAddition);
        req.setDamage(totalAbility.toString());
        player.sendUnionActivityGroupPacket(Protocol.C_CROSS_STRONGHOLD_CHALLENGE_UNION, req, req.getActivityId());

    }

    private void sendToPlayer(GamePlayer player) {
        StrongholdWarProto.AttackEliminatePatronsRespMsg.Builder resp = StrongholdWarProto.AttackEliminatePatronsRespMsg.newBuilder();
        resp.setRet(ret);
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_ELIMINATION_ATTACK, resp);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
